package com.my.menu.modelcombination.popup.actions;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.ui.dialogs.ResourceDialog;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.emf.edit.ui.action.ControlAction;
import org.eclipse.emf.edit.ui.action.LoadResourceAction;
import org.eclipse.emf.edit.ui.util.EditUIUtil;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.my.mytask.mytask.impl.ContConImpl;

import org.eclipse.bpmn2.ExtensionAttributeValue;
import org.eclipse.bpmn2.impl.ProcessImpl;
import org.eclipse.bpmn2.util.Bpmn2ResourceFactoryImpl;
import org.js.model.rbac.AccessControlModel;
import org.js.model.rbac.Role;
import org.js.model.rbac.impl.AccessControlModelImpl;

public class Test implements IObjectActionDelegate {

	private Shell shell;
	private IFile ifile;
	private ISelection iselection;
	private URI origURI;
	private ResourceSet resourceSet;
	private Resource origRes = null;
	private Resource goalRes = null;
	private final int OK = 0;
	private final int CANCEL = 1;

	/**
	 * Constructor for Action1.
	 */
	public Test() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		System.out.println("setActivePart");

		iselection = targetPart.getSite().getSelectionProvider().getSelection();
		shell = targetPart.getSite().getShell();
		// initial resource set
		resourceSet = new ResourceSetImpl();
//		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
//				.put("bpmn", new Bpmn2ResourceFactoryImpl());

		if (iselection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) iselection).getFirstElement();
			ifile = (IFile) Platform.getAdapterManager().getAdapter(obj,
					IFile.class);
			if (ifile == null) {
				if (obj instanceof IAdaptable) {
					ifile = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
				}
			}
			if (ifile != null) {
				origURI = URI.createPlatformResourceURI(ifile.getFullPath()
						.toString(), true);
				try {
					// initial original resource
					origRes = resourceSet.getResource(origURI, true);
				} catch (Exception e) {
					origRes = resourceSet.getResource(origURI, false);
				}
				try {
					origRes.load(Collections.EMPTY_MAP);
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println("before loading: "
						+ resourceSet.getResources());
			}
		}
	}

	// TODO: add delete model method
	// TODO: in the dialog only show the rbac file
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		System.out.println("run");

//		ResourceDialog rd = new ResourceDialog(shell, "Load Model", SWT.OPEN);
//		int state = rd.open();
		// ResourceSet resourceSet = editingDomain.getResourceSet();

//		if (state != CANCEL) {
//			if (rd.getURIText() != "") {
//				goalRes = resourceSet.getResource(rd.getURIs().get(0), true);
//				try {
//					goalRes.load(Collections.EMPTY_MAP);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				System.out.println(rd.getURIs().get(0).toString());
				PrintModel();
//			}
//		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		System.out.println("selectionChanged");
	}

	/**
	 * connect access control model to bpmn model
	 */
	public void PrintModel() {
		// EList<EObject> origContents =origRes.getContents();
		TreeIterator<EObject> origIt = origRes.getAllContents();
		AccessControlModel goalEle = null;
		ProcessImpl origEle = null;
		
		while (origIt.hasNext()) {
			EObject object = origIt.next();
			if (object instanceof ProcessImpl) {
					origEle = (ProcessImpl) object;
			}
		}
		
		ExtensionAttributeValue eav = ((ProcessImpl) origEle)
				.getExtensionValues().get(0);
		TreeIterator ti = eav.eAllContents();
		while(ti.hasNext()) {
			Object object = ti.next();
			if (object.getClass().equals(ContConImpl.class)) {
				goalEle=((ContConImpl) object).getAcm();
			}
		}
		if(goalEle!=null){
			goalEle=goalEle.getAccessControlModels().get(0);
			System.out.println("roles size: " + goalEle.getRoles().size());
			for(Role role:goalEle.getRoles()){
				System.out.println("role id: " + role.getId());
			}
		}else{
			System.out.println("no model loaded! ");
		}
	
	}
}
